﻿<!DOCTYPE html>
<html>
<head>
    <title>字段模版</title>
    <link rel="stylesheet" type="text/css" href="../../../Scripts/easyUI/themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="../../../Scripts/easyUI/themes/icon.css" />
    <script type="text/javascript" src="../../../Scripts/jquery/jquery.min.js"></script>
    <script type="text/javascript" src="../../../Scripts/jquery/jquery.easyui.min.js"></script>
    <script src="../../../Scripts/QueryString.js" type="text/javascript"></script>
    <script src="../../../Scripts/config.js" type="text/javascript"></script>
    <script src="../../../Comm/Gener.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">

        var fk_mapdata = GetQueryString("FK_MapData");
        var groupID = GetQueryString("GroupField");
        if (groupID == null) {
            var gfs = new Entities("BP.Sys.GroupFields");
            gfs.Retrieve("FrmID", fk_mapdata, "CtrlType", "");
            groupID = gfs.length == 0 ? 0 : gfs[0].OID;
        }

        var fType = GetQueryString("Type");

        //页面启动函数.
        $(function () {

            var fk_mapData = GetQueryString("FK_MapData");
            if (fk_mapData == null || fk_mapData == "") {
                alert("参数FK_MapData为空.");
                return;
            }

            var ens = new Entities("BP.Sys.MapDataTemplates");
            ens.Retrieve("IsTemplate", 1);

            if (ens.length == 0) {
                $("#info").html("系统没有模版，<a href='javascript:CreateFrm()' >点击这里创建模版？</a>");
                return;
            }

            var html = "";
            html += "<ul>";

            for (var i = 0; i < ens.length; i++) {
                var en = ens[i];

                var list = "<ul>";
                //查询模板字段
                var mapAttrs = new Entities("BP.Sys.MapAttrs");
                mapAttrs.Retrieve("FK_MapData", en.No);
                //查询模板字段扩展
                var mapExts = new Entities("BP.Sys.MapExts");
                mapExts.Retrieve("FK_MapData", en.No);
                for (var j = 0; j < mapAttrs.length; j++) {
                    var mapAttr = mapAttrs[j];
                    list += "<li><a href=\"javascript:AddMapAttr('" + mapAttr.KeyOfEn + "','" + mapAttr.FK_MapData + "')\" >" + mapAttr.KeyOfEn + "-" + mapAttr.Name;
                    for (var k = 0; k < mapExts.length; k++) {
                        if (mapAttr.KeyOfEn == mapExts[k].AttrOfOper) {
                            list += "-" + mapExts[k].ExtType;
                        }
                    }
                    list += "</a></li>";
                }

                list += "</ul>";

                var edit = "<a href='../Designer.htm?FK_MapData=" + en.No + "' target=_blank>编辑模版</a>";

                html += "<li>" + en.Name + " - " + edit + list + "</li>";
            }
            html += "</ul>";

            $("#info").html(html);

        });

        //把这个字段增加到里面去.
        function AddMapAttr(keyOfEn, fk_mapdata) {

            //首先判断该字段是否存在？如果存在就让其修改名称.
            var mypkOfMapAttr = fk_mapdata + "_" + keyOfEn;
            var en = new Entity("BP.Sys.MapAttr", mypkOfMapAttr);

            var name = promptGener('请输入要增加的字段名称', en.Name);
            if (name == undefined || name == null)
                return;

            var mapAttrs = new Entities("BP.Sys.MapAttrs");
            mapAttrs.Retrieve("FK_MapData", GetQueryString("FK_MapData"), "Name", name);
            if (mapAttrs.length >= 1) {
                alert('名称：[' + name + "]已经存在.");
                return;
            }

            var field = promptGener('字段名', keyOfEn);
            if (field == undefined || field == null)
                return;

            en.KeyOfEn = field;
            en.Name = name;
            en.MyPK = GetQueryString("FK_MapData") + "_" + field;
            en.SetPKVal(en.MyPK);
            if (en.IsExits() == true) {
                alert('字段:' + field + "已经存在.");
                return;
            }

            //增加新字段.
            en.FK_MapData = GetQueryString("FK_MapData");
            en.KeyOfEn = field;
            en.UIHeight = en.UIHeight || 23;
            en.UIWidth = en.UIWidth || 100;
            en.GroupID = groupID ;
            en.Idx = 0;
            en.Insert();
            alert('字段插入成功' + en.MyPK);

            var MapExts = new Entities("BP.Sys.MapExts");
            MapExts.Retrieve("FK_MapData", fk_mapdata, "AttrOfOper", keyOfEn);
            for (var i = 0; i < MapExts.length; i++) {
                var mapExt = MapExts[i];
                var en = new Entity("BP.Sys.MapExt", mapExt);
                en.FK_MapData = GetQueryString("FK_MapData");
                en.MyPK = mapExt.MyPK;
                en.MyPK = en.MyPK.replace(fk_mapdata, GetQueryString("FK_MapData"));
                en.MyPK = en.MyPK.replace(keyOfEn, field); //替换字段.
                en.AttrOfOper = field;
                en.SetPKVal(en.MyPK);
                en.Insert();
            }

            if (window.parent != null) {
                window.parent.location.href = window.parent.location.href + "&IsFirst=1";
            }

            window.close();
        }


        //创建模版.
        function CreateFrm() {

            var en = new Entity("BP.Sys.MapDataTemplate");

            var name = promptGener('请输入模版的名称', '我的字段模版');
            if (name == undefined || name == "")
                return;

            var ens = new Entities("BP.Sys.MapDataTemplates");
            ens.Retrieve("IsTemplate", 1);

            en.No = "Temp" + ens.length;
            en.Name = name;
            en.IsTemplate = 1;
            en.Insert();

            var url = "../Designer.htm?FK_MapData=" + en.No;
            window.open(url);
        }

        //字段分组.
        function Group() {

            var url = "FieldsGroup.htm?FK_MapData=" + GetQueryString("FK_MapData");
            SetHref(url);
        }

        function Fields() {

            var url = "Fields.htm?FK_MapData=" + GetQueryString("FK_MapData");
            SetHref(url);

        }
    </script>
    <!--<base target="_self" /> -->
    <link href="../../../../DataUser/Style/ccbpm.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div style="padding-left:30px">
        <h3><a href="javascript:Fields();">字段模版</a> -字段群模版</h3>
        <div id="info">
        </div>
    </div>


</body>
</html>
